package com.example.backend;

import java.sql.*;

public class DatabaseSetup {

    // 数据库连接信息
    private static final String URL = "jdbc:mysql://localhost:3306/game_message";  // 直接连接到 game_message 数据库
    private static final String USER = "root";
    private static final String PASSWORD = "your_password_for_mysql";

    // 创建数据库的SQL
    private static final String CREATE_DB_SQL = "CREATE DATABASE IF NOT EXISTS game_message";

    // 创建localmeassage表的SQL
    private static final String CREATE_LOCALMEASSAGE_TABLE_SQL = "CREATE TABLE IF NOT EXISTS game_message.localmeassage ("
            + "username VARCHAR(255), "
            + "weapon_names VARCHAR(255), "
            + "gametime INT, "
            + "clearancestatus VARCHAR(255), "
            + "clearancetime VARCHAR(255)"
            + ")";

    // 创建weapons_levels表的SQL
    private static final String CREATE_WEAPONS_LEVELS_TABLE_SQL = "CREATE TABLE IF NOT EXISTS game_message.weapons_levels ("
            + "weapon_name VARCHAR(255), "
            + "weapon_level INT"
            + ")";

    // 检查数据库是否存在
    private static boolean isDatabaseExists(Connection connection, String dbName) throws SQLException {
        DatabaseMetaData metaData = connection.getMetaData();
        ResultSet resultSet = metaData.getCatalogs();
        while (resultSet.next()) {
            if (resultSet.getString(1).equals(dbName)) {
                return true;
            }
        }
        return false;
    }

    // 检查表格是否存在
    private static boolean isTableExists(Connection connection, String tableName) throws SQLException {
        ResultSet resultSet = connection.getMetaData().getTables(null, null, tableName, null);
        return resultSet.next();
    }

    // 数据插入接口
    public static void insertLocalmeassageData(String username, String weaponNames, int gametime, String clearanceStatus, String clearanceTime) {
        String insertSQL = "INSERT INTO game_message.localmeassage (username, weapon_names, "
                + "gametime, clearancestatus, clearancetime) "
                + "VALUES (?, ?, ?, ?, ?)";
        try (Connection connection = DriverManager.getConnection(URL, USER, PASSWORD);
             PreparedStatement statement = connection.prepareStatement(insertSQL)) {
            statement.setString(1, username);
            statement.setString(2, weaponNames);
            statement.setInt(3, gametime);
            statement.setString(4, clearanceStatus);
            statement.setString(5, clearanceTime);
            statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void insertWeaponsLevelsData(String weaponName, int weaponLevel) {
        String insertSQL = "INSERT INTO game_message.weapons_levels (weapon_name, weapon_level) "
                + "VALUES (?, ?)";
        try (Connection connection = DriverManager.getConnection(URL, USER, PASSWORD);
             PreparedStatement statement = connection.prepareStatement(insertSQL)) {
            statement.setString(1, weaponName);
            statement.setInt(2, weaponLevel);
            statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    // 创建数据库和表
    public static void createDatabaseAndTables() {
        try (Connection connection = DriverManager.getConnection(URL, USER, PASSWORD)) {
            // 判断数据库是否存在，如果不存在则创建
            if (!isDatabaseExists(connection, "game_message")) {
                try (Statement statement = connection.createStatement()) {
                    statement.executeUpdate(CREATE_DB_SQL);  // 创建数据库
                    System.out.println("Database created successfully.");
                }
            }

            // 切换到 game_message 数据库
            try (Statement statement = connection.createStatement()) {
                statement.executeUpdate("USE game_message");  // 切换数据库
            }

            // 创建表格
            try (Statement statement = connection.createStatement()) {
                if (!isTableExists(connection, "localmeassage")) {
                    statement.executeUpdate(CREATE_LOCALMEASSAGE_TABLE_SQL);  // 创建localmeassage表
                    System.out.println("Table localmeassage created successfully.");
                }
                if (!isTableExists(connection, "weapons_levels")) {
                    statement.executeUpdate(CREATE_WEAPONS_LEVELS_TABLE_SQL);  // 创建weapons_levels表
                    System.out.println("Table weapons_levels created successfully.");
                }
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        // 连接到MySQL并创建数据库和表
        createDatabaseAndTables();

        // 示例插入数据
        insertLocalmeassageData("SJJ", "A1,B1,C1,D1", 120, "1 1 1 0", "1500 1800 1300 -1");
        insertWeaponsLevelsData("A1", 5);
        insertWeaponsLevelsData("B1", 4);
        insertWeaponsLevelsData("C1", 3);
        insertWeaponsLevelsData("D1", 6);
    }
}
